package com.txzt.gaea.provide.utils;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;

public class BeanConvertUtils extends BeanUtils {



    public static <S, T> T convertTo(S source, Supplier<T> targetSupplier, ConvertCallBack<S, T> callBack) throws Exception {
        if (source ==null || targetSupplier == null) {
            return null;
        }

        T target = targetSupplier.get();
        copyProperties(source, target);
        if (callBack != null) {
            callBack.callback(source, target);
        }

        return target;
    }


    /**
     * 转换对象
     *
     * @param sources 源list
     * @param targetSupplier 目标对象供应方
     * @param callBack 回调方法
     * @param <S> 源对象类型
     * @param <T> 目标对象类型
     * @return
     * @throws Exception
     */
    public static <S, T> List<T> convertListTo(List<S> sources, Supplier<T> targetSupplier, ConvertCallBack<S, T> callBack) throws Exception {
        if (CollectionUtils.isEmpty(sources) || targetSupplier == null) {
            return null;
        }

        List<T> targetList = new ArrayList<>();

        for (S source : sources) {
            T target = targetSupplier.get();
            copyProperties(source, target);
            if (callBack != null) {
                callBack.callback(source, target);
            }
            targetList.add(target);
        }
        return targetList;
    }

    @FunctionalInterface
    public interface ConvertCallBack<S, T> {
        void callback(S t, T s);
    }


    public static <S, T> List<T> convertListTo(List<S> sources, Supplier<T> targetSupplier) throws Exception{
        return convertListTo(sources, targetSupplier, null);
    }


    public static void main(String[] args) throws Exception {

        class A{
            String name;
            Integer age;
            String address;

            public String getName() {
                return name;
            }

            public void setName(String name) {
                this.name = name;
            }

            public Integer getAge() {
                return age;
            }

            public void setAge(Integer age) {
                this.age = age;
            }

            public String getAddress() {
                return address;
            }

            public void setAddress(String address) {
                this.address = address;
            }
        }

        class B{
            String name;
            Integer age;
            String address1;

            public String getName() {
                return name;
            }

            public void setName(String name) {
                this.name = name;
            }

            public Integer getAge() {
                return age;
            }

            public void setAge(Integer age) {
                this.age = age;
            }

            public String getAddress1() {
                return address1;
            }

            public void setAddress1(String address1) {
                this.address1 = address1;
            }
        }

        A a = new A();
        a.setName("zhangsan");
        a.setAge(21);
        a.setAddress("wuhan");
        A a1 = new A();
        a1.setName("lisi");
        a1.setAge(22);
        A a2 = new A();
        a2.setName("wanger");
        a2.setAge(23);

        List<A> as = new ArrayList<>();
        as.add(a1);
        as.add(a);
        as.add(a2);


        //List<B> list = convertListTo(as, B::new);

        B b = convertTo(a, B::new, (s, t)->{
            t.setAddress1(s.getAddress());
        });

        System.out.println();


//        B b = new B();
//
//        copyProperties(a, b);
//        System.out.println();











    }


}
